// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  data: {
    addressData: [],
    state: '',
    // 
    addressId: 0,
    // 
    addFlag: true,
    color: "#808080",
    region: ['', '', ''],
    // 
    updateFlag: true,
    updateName: "",
    updatePhone: "",
    updateArea: "",
    updateDetail: "",
    // 
    uid: "",
    userType: "",
    disabled: {
      type: "",
      value: "",
    },
    detailAddr: '',//详细地址
  },

  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })
    this.GetMyAddress();
  },

  // 收货地址列表
  GetMyAddress: function () {
    var that = this;
    var uid = this.data.uid;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetMyAddress&UID=" + uid),
      success: function (res) {
        var addressData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(addressData);
        for (var i = 0; i < addressData.length; i++) {
          addressData[i].Address = addressData[i].Address.replace(/%23/g, ' ');
        }
        that.setData({
          addressData: addressData,
        })
      }
    })
  },
  // 
  addTap0: function () {
    this.setData({
      addFlag: !this.data.addFlag,
    })
  },
  // 
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value,
      color: "#333",
    })
  },
  // 添加收货地址
  addFormSubmit: function (e) {
    var that = this;
    var uid = this.data.uid;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AddAddress&UID=" + uid + "&UserName=" + e.detail.value.name + "&tel=" + e.detail.value.phone + "&Address=" + e.detail.value.area[0] + " " + e.detail.value.area[1] + " " + e.detail.value.area[2] + "&Memo=" + e.detail.value.detailAddress),
      success: function (res) {
        if (JSON.parse(baseJs.decryUrl(res.data)).ResultFlag == 1) {
          wx.showToast({
            title: JSON.parse(baseJs.decryUrl(res.data)).ResultMsg,
            icon: 'none',
            duration: 1000
          });
          that.GetMyAddress();
          setTimeout(function () {
            that.setData({
              addFlag: !that.data.addFlag,
            })
        
          }, 1500)
        } else {
          wx.showToast({
            title: JSON.parse(baseJs.decryUrl(res.data)).ResultMsg,
            icon: 'none',
            duration: 1000
          });
        }
      }
    })

  },
  // 
  addFormReset: function () {
    this.setData({
      addFlag: !this.data.addFlag,
    })
  },
  // 删除地址
  delTap: function (e) {
    var that = this;
    var uid = this.data.uid;
    var addressId = this.data.addressId;
    addressId = e.currentTarget.dataset.addressid;
    this.setData({
      addressId: addressId,
    })
    wx.showModal({
      title: '提示',
      content: '确认删除该地址吗',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=DelAddress&UID=" + uid + "&ID=" + addressId),
            success: function (res) {
              if (JSON.parse(baseJs.decryUrl(res.data)).ResultFlag == 1) {
                wx.showToast({
                  title: JSON.parse(baseJs.decryUrl(res.data)).ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                that.GetMyAddress();
              } 
            }
          })
        }else if(res.cancel){
          console.log("取消");
        }
      }
    })
  },
  // 设置默认
  setDefaultAddress: function (e) {
    console.log(e);
    var that = this;
    var uid = this.data.uid;
    var addressId = this.data.addressId;
    addressId = e.currentTarget.dataset.addressid;
    this.setData({
      addressId: addressId,
    })
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=UpDefaultAddress&UID=" + uid + "&ID=" + addressId),
      success: function (res) {
        // console.log(JSON.parse(baseJs.decryUrl(res.data)));
        if (JSON.parse(baseJs.decryUrl(res.data)).ResultFlag == 1) {
          wx.showToast({
            title: JSON.parse(baseJs.decryUrl(res.data)).ResultMsg,
            icon: 'none',
            duration: 1000
          });
          that.GetMyAddress();
        }
      }
    })
  },
  // 修改收货地址
  updateTap: function (e) {
    var updateName = this.data.updateName;
    var updatePhone = this.data.updatePhone;
    var updateArea = this.data.updateArea;
    var updateDetail = this.data.updateDetail;
    var addressId = this.data.addressId;
    updateName = e.currentTarget.dataset.name;
    updatePhone = e.currentTarget.dataset.phone;
    updateArea = e.currentTarget.dataset.area;
    updateDetail = e.currentTarget.dataset.detail;
    addressId = e.currentTarget.dataset.addressid;
    this.setData({
      updateName: updateName,
      updatePhone: updatePhone,
      updateArea: updateArea,
      updateDetail: updateDetail,
      addressId: addressId,
      updateFlag: !this.data.updateFlag,
    })
  },
  // 修改收货地址
  updateFormSubmit: function (e) {
    var that = this;
    var uid = this.data.uid;
    var addressId = this.data.addressId;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=UpAddress&UID=" + uid + "&UserName=" + e.detail.value.updateName + "&tel=" + e.detail.value.updatePhone + "&Address=" + e.detail.value.updateArea + "&Memo=" + e.detail.value.updateDetailAddress + "&ID=" + addressId),
      success: function (res) {
        if (JSON.parse(baseJs.decryUrl(res.data)).ResultFlag == 1) {
          wx.showToast({
            title: JSON.parse(baseJs.decryUrl(res.data)).ResultMsg,
            icon: 'none',
            duration: 2000
          });
          that.GetMyAddress();
          setTimeout(function () {
            that.setData({
              updateFlag: !that.data.updateFlag,
            })
          }, 2000)
        } else {
          wx.showToast({
            title: JSON.parse(baseJs.decryUrl(res.data)).ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })

  },
  updateFormReset: function () {
    this.setData({
      updateFlag: !this.data.updateFlag,
    })
  },
  // 详细地址选择定位
  chooseAdd() {
    var that = this
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.address']) { //判断用户是否获取定位权限
          wx.chooseLocation({
            success: res => {
              console.log(res);
              var reg = /.+?(省|市|自治区|自治州|县|区)/g;
              var addr = res.address.match(reg)
              that.setData({
                'region[0]': addr[0],
                'region[1]': addr[1],
                'region[2]': addr[2],
                detailAddr: res.name
              })
            },
            fail: err => {
              console.log(err);
            }
          })
        } else {
          wx.showToast({
            title: '授权失败',
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },

})